package com.jhws.sys.cms.controller;

import com.jhws.common.base.bo.JsonRespData;
import com.jhws.sys.cms.bo.NodePrivateInfo;
import com.jhws.sys.cms.service.NodePrivateInfoService;
import com.jhws.sys.user.bo.TblUserDimBo;
import com.jhws.sys.user.service.ITblUserDimService;
import com.jhws.util.StringUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

@Controller
@RequestMapping("/cms/nodePrivateInfo")
public class NodeAliasesController {
    @Resource
    private NodePrivateInfoService nodeAliasesService;
    @Resource
    private ITblUserDimService userDimService;

    @RequestMapping("saveNodePrivateInfo.x")
    @ResponseBody
    public JsonRespData saveNodePrivateInfo(@RequestParam Map param) {
        String nodeAliase = MapUtils.getString(param, "nodeAliase");
        String typeCode = MapUtils.getString(param, "typeCode");
        String customProperty = MapUtils.getString(param, "customProperty");
        int unreadDefComm = MapUtils.getIntValue(param, "unreadDefComm");
        TblUserDimBo userDimBo = userDimService.currentUser();
        NodePrivateInfo nodePrivateInfo = new NodePrivateInfo();
        if (StringUtils.isEmpty(nodeAliase)) {
            nodeAliasesService.deleteAliases("where " +
                    "user='" + userDimBo.getLogName() + "' and type_code='" + typeCode + "'");
        } else {
            nodePrivateInfo.setAliases(nodeAliase);
        }

        nodePrivateInfo.setCustomProperty(customProperty);
        nodePrivateInfo.setUser(userDimBo.getLogName());
        nodePrivateInfo.setTypeCode(typeCode);
        nodePrivateInfo.setUnreadDefComm(unreadDefComm);
        //保存节点别名
        nodeAliasesService.save(nodePrivateInfo);
        return new JsonRespData("", 1, nodePrivateInfo);
    }
}
